package ${domain}.config;

import ${domain}.frame.auth.LocalData;
import ${domain}.frame.schedule.RunTask;
import ${domain}.frame.schedule.Scheduler;
import ${domain}.frame.utils.LogUtil;
import ${domain}.module.wsys.ent.TaskSql;
import ${domain}.module.wsys.mgr.TaskSqlManager;
import ${domain}.module.wsys.req.TaskSqlFindRequest;
import ${domain}.module.wsys.rsp.TaskSqlFindResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AssignableTypeFilter;

import javax.annotation.PostConstruct;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Configuration
public class TaskConfig {

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private TaskSqlManager taskSqlManager;

    @Bean
    public Scheduler registerScheduler() {
        return new Scheduler();
    }

    @PostConstruct
    public void registryTask() {
        {// 扫描类任务
            String aPackage = this.getClass().getPackage().getName();
            Pattern compile = Pattern.compile("(.*)\\.config");
            Matcher matcher = compile.matcher(aPackage);
            if (matcher.find()) {
                DefaultListableBeanFactory simpleBeanDefinitionRegistry = new DefaultListableBeanFactory();
                ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(simpleBeanDefinitionRegistry);
                classPathBeanDefinitionScanner.resetFilters(false);
                classPathBeanDefinitionScanner.addIncludeFilter(new AssignableTypeFilter(RunTask.class));
                classPathBeanDefinitionScanner.setBeanNameGenerator(new BeanNameGenerator() {
                    @Override
                    public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) {
                        String beanClassName = beanDefinition.getBeanClassName();
                        try {
                            Class<?> aClass = Class.forName(beanClassName);
                            Object instance = aClass.newInstance();
                            RunTask task = (RunTask) instance;
                            scheduler.createOrRepeat(task);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtil.i("registry task " + beanClassName);
                        return beanClassName;
                    }
                });
                classPathBeanDefinitionScanner.scan(matcher.group(1));
            }
        }

        {// 实例化SQL任务
            TaskSqlFindRequest taskSqlFindRequest = new TaskSqlFindRequest();
            taskSqlFindRequest.setValid(true);
            taskSqlFindRequest.setPageSize(0);
            TaskSqlFindResponse taskSqlFindResponse = taskSqlManager.find(taskSqlFindRequest, LocalData.getSysToken());
            for (TaskSql taskSql : taskSqlFindResponse.getResult()) {
                taskSqlManager.createOrRepeat(taskSql);
            }
        }
    }
}